//-----------------------------------------------------------------------
// <copyright file="ConPointDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class ConPointDAOBase
    {
        #region Common methods
        public virtual ConPoint CreateConPointFromReader(IDataReader reader)
        {
            ConPoint item = new ConPoint();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ConPointID"))) item.ConPointID = (int)reader["ConPointID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("Goods"))) item.Goods = (string)reader["Goods"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDisplayed"))) item.IsDisplayed = (bool)reader["IsDisplayed"];
                if (!reader.IsDBNull(reader.GetOrdinal("ContainerLoading"))) item.ContainerLoading = (string)reader["ContainerLoading"];
                if (!reader.IsDBNull(reader.GetOrdinal("LoadingPort"))) item.LoadingPort = (string)reader["LoadingPort"];
				if (!reader.IsDBNull(reader.GetOrdinal("TallPalletVol"))) item.TallPalletVol = (decimal)reader["TallPalletVol"];
				if (!reader.IsDBNull(reader.GetOrdinal("HalfPalletVol"))) item.HalfPalletVol = (decimal)reader["HalfPalletVol"];

            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateConPointFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateConPoint methods
            
        public virtual void CreateConPoint(ConPoint conPoint)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spConPointsInsert");
                
                dbCommandWrapper.AddInParameter("@Description", DbType.String, conPoint.Description);
                dbCommandWrapper.AddInParameter("@Goods", DbType.String, conPoint.Goods);
                dbCommandWrapper.AddInParameter("@IsDisplayed", DbType.Boolean, conPoint.IsDisplayed);
                dbCommandWrapper.AddInParameter("@ContainerLoading", DbType.String, conPoint.ContainerLoading);
                dbCommandWrapper.AddInParameter("@LoadingPort", DbType.String, conPoint.LoadingPort);
				dbCommandWrapper.AddInParameter("@TallPalletVol", DbType.Currency, conPoint.TallPalletVol==Nulls.NullDecimal?DBNull.Value:(object)conPoint.TallPalletVol);
				dbCommandWrapper.AddInParameter("@HalfPalletVol", DbType.Currency, conPoint.HalfPalletVol==Nulls.NullDecimal?DBNull.Value:(object)conPoint.HalfPalletVol);
                dbCommandWrapper.AddOutParameter("@ConPointID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                conPoint.ConPointID = (int)dbCommandWrapper.GetParameterValue("@ConPointID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateConPointException, ex);
            }
        }

        #endregion

        #region UpdateConPoint methods
        
        public virtual void UpdateConPoint(ConPoint conPoint)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spConPointsUpdate");            
                
                dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, conPoint.ConPointID);
                dbCommandWrapper.AddInParameter("@Description", DbType.String, conPoint.Description);
                dbCommandWrapper.AddInParameter("@Goods", DbType.String, conPoint.Goods);
                dbCommandWrapper.AddInParameter("@IsDisplayed", DbType.Boolean, conPoint.IsDisplayed);
                dbCommandWrapper.AddInParameter("@ContainerLoading", DbType.String, conPoint.ContainerLoading);
                dbCommandWrapper.AddInParameter("@LoadingPort", DbType.String, conPoint.LoadingPort);
				dbCommandWrapper.AddInParameter("@TallPalletVol", DbType.Currency, conPoint.TallPalletVol==Nulls.NullDecimal?DBNull.Value:(object)conPoint.TallPalletVol);
				dbCommandWrapper.AddInParameter("@HalfPalletVol", DbType.Currency, conPoint.HalfPalletVol==Nulls.NullDecimal?DBNull.Value:(object)conPoint.HalfPalletVol);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateConPointException, ex);
            }
        }
        
        #endregion

        #region DeleteConPoint methods
        public virtual void DeleteConPoint(int conPointID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spConPointsDelete");
                
                dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, conPointID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteConPointException, ex);
            }
        }

        #endregion

        #region GetConPoint methods
        
        public virtual ConPoint GetConPoint(int conPointID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spConPointsGet");
                
                dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, conPointID);
                
                ConPoint conPoint =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        conPoint = CreateConPointFromReader(reader);
                        reader.Close();
                    }
                }
                return conPoint;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetConPointException, ex);
            }
        }
        
        #endregion

        #region GetConPoints methods
        public virtual ConPointCollection GetConPoints(ConPointColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spConPointsGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                ConPointCollection conPointCollection = new ConPointCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        ConPoint conPoint = CreateConPointFromReader(reader);
                        conPointCollection.Add(conPoint);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return conPointCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetConPointsException, ex);
            }
        }
        
        public virtual ConPointCollection GetConPoints(ConPointColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetConPoints(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}